package cn.flaty.views.web.upload;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import cn.flaty.utils.web.file.AbstractFileUpload;
import cn.flaty.utils.web.file.SpringMultipartFileInfo;
import cn.flaty.utils.web.file.UploadInfo;
import cn.flaty.utils.web.file.UploadResult;
import cn.flaty.views.web.BaseController;


/**
 * 文件上传DEMO
 * @author flatychen
 * @date 2014-4-21
 */
@Controller
@RequestMapping("/files")
public class FileBaseController extends BaseController {

	@Resource(name="sampleFileupload")
	private AbstractFileUpload fileUpload;
	
	
	/**
	 * 上传文件
	 * @author flatychen
	 * @date 2014-4-21
	 * @return
	 */
	@RequestMapping(value = "/new", method = RequestMethod.GET)
	public String register() {
		return "/views/files/upload";
	}
	

	/**
	 * 上传文件处理
	 * @author flatychen
	 * @date 2014-4-21
	 * @param request
	 * @param files
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/create", method = RequestMethod.POST)
	public String create(HttpServletRequest request,@RequestParam("fileName") MultipartFile[]  files,Model model) {
		
		UploadInfo[] uploadInfos = new UploadInfo[files.length];
		
		
		
		
		
		for (int i = 0; i < uploadInfos.length; i++) {
			uploadInfos[i] = new SpringMultipartFileInfo(files[i],super.getWebRootPath(request));
		}
		// 上传文件
		List<UploadResult> ups = fileUpload.uploadFiles(uploadInfos);
		
		//model.addAttribute(ups);
		model.addAttribute("data",ups);
		//model.addAttribute("test", false);
		return "/views/files/shows";
//		UploadInfo uploadInfo = new SpringMultipartFileInfo(file);
//		// 上传文件
//		UploadResult up = UploadUtils.uploadFile(super.getWebRootPath(request), uploadInfo, fileRepo);
//		model.addAttribute("data",up);
//		return "/views/files/show";
	}

	
	
	/**
	 * 一个文件表单上传多个
	 * @author flatychen
	 * @date 2014-4-21
	 * @return
	 */
	@RequestMapping(value = "/news", method = RequestMethod.GET)
	public String news() {
		return "/views/files/uploads";
	}
	
	
	/**
	 * 一个文件表单上传多个处理
	 * @author flatychen
	 * @date 2014-4-21
	 * @param request
	 * @param files
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/creates", method = RequestMethod.POST)
	public String creates(HttpServletRequest request,@RequestParam("fileName") MultipartFile[]  files,Model model) {
		UploadInfo[] uploadInfos = new UploadInfo[files.length];
		for (int i = 0; i < uploadInfos.length; i++) {
			uploadInfos[i] = new SpringMultipartFileInfo(files[i],super.getWebRootPath(request));
		}
		// 上传文件
		List<UploadResult> ups = fileUpload.uploadFiles(uploadInfos);
		
		//model.addAttribute(ups);
		model.addAttribute("data",ups);
		model.addAttribute("test", false);
		return "/views/files/shows";
		// 上传文件
//		List<UploadResult> ups = UploadUtils.uploadFiles(super.getWebRootPath(request), uploadInfos, fileRepo);
//		model.addAttribute(ups);
		//model.addAttribute("data",ups);
	}

}
